package com.longcai.util.aspect;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.UUID;

import com.longcai.entity.sys.SysLog;
import com.longcai.entity.sys.SysUser;
import com.longcai.service.SysLogService;
import com.longcai.util.*;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 定义切面
 *
 * @author zhaojie
 * @Description: TODO
 * @file:
 * @throws
 * @date 2018/11/20 15:35
 */
@Aspect
@Component
public class LoggerAspect {

    private static final Logger logger = LoggerFactory.getLogger(LoggerAspect.class);

    @Autowired
    public SysLogService sysLogService;

    /**
     * 定义切入点
     */
    /*@Pointcut("@annotation(com.longcai.util.aspect.Log)")*/
    @Pointcut("execution( * com.longcai..controller.*.*(..))")
    public void logPointCut() {
    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        /*long beginTime = System.currentTimeMillis();*/
        long CTime = System.currentTimeMillis();
        // 执行方法
        Object result = point.proceed();
        // 执行时长(毫秒)
        long time = System.currentTimeMillis() - CTime;
        /*  long time = System.currentTimeMillis() - beginTime;*/
        //异步保存日志
        save(point, time);
        return result;
    }

    void save(ProceedingJoinPoint joinPoint, long time) throws Exception {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SysLog sysLog = new SysLog();
        Log log = method.getAnnotation(Log.class);
        if (log != null) {
            // 注解上的描述
            sysLog.setOperation(log.value());
        }
        // 请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLog.setMethod(className + "." + methodName + "()");

        // 请求的参数
        Object[] args = joinPoint.getArgs();
        try {
            String params = JsonUtils.beanToJson(args[0]).substring(0, 4999);
            sysLog.setParams(params);
        } catch (Exception e) {

        }
        // 获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();

        // 设置IP地址
        sysLog.setIp(IpUtils.getIpAddr(request));

        //获取当前用户
        LoginUser loginUser = UserUtil.getLoginUser();
        if (loginUser != null) {
            String username = loginUser.getUsername();
            sysLog.setUserId(loginUser.getId().toString());
            sysLog.setUsername(loginUser.getUsername());
            sysLog.setIp(sysLog.getIp());

        }

        sysLog.setId(UUID.randomUUID().toString());
        sysLog.setTime((int) time);
        // 系统当前时间
        sysLog.setCTime(new Date());
        // 保存系统日志
        this.sysLogService.save(sysLog);
    }
}